package com.ym.b2b.product.service.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ym.b2b.commons.exception.BaseException;
import com.ym.b2b.commons.service.BaseService;
import com.ym.b2b.product.dao.EmBProductPictureDAO;
import com.ym.b2b.product.service.EmBProductPictureService;
import com.ym.b2b.product.sqlbean.EmBProductPicture;
import com.zc.commons.base.Result;

@Service
public class EmBProductPictureServiceImpl extends BaseService implements EmBProductPictureService {

    @Autowired
    private EmBProductPictureDAO emBProductPictureDAO;

    @Transactional
    @Override
    public Result insertEmBProductPictureBatch(List<EmBProductPicture> list) throws BaseException {
        Result ret = new Result();
        if (CollectionUtils.isEmpty(list)) {
            ret.setCodeResult(Result.CODE_100, "未选择任何图片!");
            return ret;
        }
        try {
            Long pictureId = 0l;
            for (EmBProductPicture b : list) {

                // 判断重复
                EmBProductPicture sv = new EmBProductPicture();
                sv.setUploadId(b.getUploadId());
                sv.setProductCode(b.getProductCode());
                List<EmBProductPicture> l = emBProductPictureDAO.searchEmBProductPicture(sv);
                if (CollectionUtils.isNotEmpty(l)) {
                    continue;
                }
                emBProductPictureDAO.insertEmBProductPicture(b);
                pictureId = b.getPictureId();
            }

            doDefaultPicture(list.get(0).getProductCode());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException("图片记录插入【SQL】异常");
        }
        return ret;
    }

    @Override
    public Result deleteBatchEmBProductPicture(EmBProductPicture e1) throws BaseException {
        Result ret = new Result();
        if (CollectionUtils.isEmpty(e1.getList())) {
            ret.setCodeResult(Result.CODE_100, "未选择任何图片!");
            return ret;
        }
        try {
            for (EmBProductPicture b : e1.getList()) {
                emBProductPictureDAO.deleteEmBProductPicture(b.getPictureId());
            }

            doDefaultPicture(e1.getProductCode());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException("图片记录插入【SQL】异常");
        }
        return ret;
    }

    // 处理 产品图片默认的 问题
    private void doDefaultPicture(String productCode) throws BaseException {
        Integer isExist = emBProductPictureDAO.codeHasDefault(productCode);
        if (isExist == 0) {
            emBProductPictureDAO.doDefaultPicture(productCode);
        }

    }

    @Transactional
    @Override
    public Result updateEmBProductPictureDefault(EmBProductPicture em) throws BaseException {
        Result ret = new Result();
        try {

            // 设置 都不为默认
            EmBProductPicture em1 = new EmBProductPicture();
            em1.setProductCode(em.getProductCode());
            em1.setIsDefault(0);
            emBProductPictureDAO.updateEmBProductPicture(em1);
            // 设置当前默认

            emBProductPictureDAO.updateEmBProductPicture(em);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException("设置默认图片失败：" + e.getMessage());
        }
        return ret;
    }
}
